{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pmbus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T20:47:47.142306Z",
     "start_time": "2020-05-19T20:47:47.066312Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'in1': Rail {name=in1, voltage=Sensor {name=in1_voltage, value=1.195V}},\n",
       " 'in10': Rail {name=in10, voltage=Sensor {name=in10_voltage, value=1.798V}},\n",
       " 'in11': Rail {name=in11, voltage=Sensor {name=in11_voltage, value=1.098V}},\n",
       " 'in2': Rail {name=in2, voltage=Sensor {name=in2_voltage, value=1.796V}},\n",
       " 'in3': Rail {name=in3, voltage=Sensor {name=in3_voltage, value=0.845V}},\n",
       " 'in4': Rail {name=in4, voltage=Sensor {name=in4_voltage, value=0.845V}},\n",
       " 'in5': Rail {name=in5, voltage=Sensor {name=in5_voltage, value=1.793V}},\n",
       " 'in6': Rail {name=in6, voltage=Sensor {name=in6_voltage, value=1.795V}},\n",
       " 'in7': Rail {name=in7, voltage=Sensor {name=in7_voltage, value=0.841V}},\n",
       " 'in8': Rail {name=in8, voltage=Sensor {name=in8_voltage, value=0.841V}},\n",
       " 'in9': Rail {name=in9, voltage=Sensor {name=in9_voltage, value=0.841V}}}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pynq import pmbus\n",
    "rails = pmbus.get_rails()\n",
    "rails"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# smbus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import smbus\n",
    "\n",
    "i2c_6 = smbus.SMBus(6) \n",
    "\n",
    "PMIC_ADDR = 0x43\n",
    "PAGE = 0x00\n",
    "VOUT_MAX = 0x24\n",
    "VOUT_MARGIN_HIGH = 0x25\n",
    "VOUT_OV_FAULT_LIMIT = 0x40\n",
    "VOUT_OV_WARN_LIMIT = 0x42\n",
    "VOUT_COMMAND = 0x21\n",
    "VOUT_UV_WARN_LIMIT = 0x43\n",
    "VOUT_UV_FAULT_LIMIT = 0x44\n",
    "VOUT_MARGIN_LOW = 0x26\n",
    "POWER_GOOD_OFF = 0x5F\n",
    "POWER_GOOD_ON = 0x5E\n",
    "\n",
    "PAGE_01 = 0x01\n",
    "VOUT_MAX_1894V = 0x01E4\n",
    "VOUT_MARGIN_HIGH_1855V = 0x01DA\n",
    "VOUT_OV_FAULT_LIMIT_1953V = 0x01F3\n",
    "VOUT_OV_WARN_LIMIT_1902V = 0x01E6\n",
    "VOUT_COMMAND_1801V = 0x01CD\n",
    "VOUT_UV_WARN_LIMIT_1702V = 0x01B3\n",
    "VOUT_UV_FAULT_LIMIT_1651V = 0x01A6\n",
    "VOUT_MARGIN_LOW_1753V = 0x01C0\n",
    "POWER_GOOD_OFF_1603V = 0x019A\n",
    "POWER_GOOD_ON_1702V = 0x01B3\n",
    "\n",
    "i2c_6.write_byte_data(PMIC_ADDR, PAGE, PAGE_01)\n",
    "temp_reg = i2c_6.read_byte_data(PMIC_ADDR, PAGE)\n",
    "print(\"Set PAGE 0x%X %s.\" %(PAGE_01, \"successfully\"if temp_reg == PAGE_01 else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_MAX, VOUT_MAX_1894V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_MAX)\n",
    "print(\"Set VOUT_MAX 1.894V %s.\" %(\"successfully\"if temp_reg == VOUT_MAX_1894V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_MARGIN_HIGH, VOUT_MARGIN_HIGH_1855V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_MARGIN_HIGH)\n",
    "print(\"Set VOUT_MARGIN_HIGH 1.855V %s.\" %(\"successfully\"if temp_reg == VOUT_MARGIN_HIGH_1855V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_OV_FAULT_LIMIT, VOUT_OV_FAULT_LIMIT_1953V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_OV_FAULT_LIMIT)\n",
    "print(\"Set VOUT_OV_FAULT_LIMIT 1.953V %s.\" %(\"successfully\"if temp_reg == VOUT_OV_FAULT_LIMIT_1953V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_OV_WARN_LIMIT, VOUT_OV_WARN_LIMIT_1902V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_OV_WARN_LIMIT)\n",
    "print(\"Set VOUT_OV_WARN_LIMIT 1.902V %s.\" %(\"successfully\"if temp_reg == VOUT_OV_WARN_LIMIT_1902V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_COMMAND, VOUT_COMMAND_1801V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_COMMAND)\n",
    "print(\"Set VOUT_COMMAND 1.801V %s.\" %(\"successfully\"if temp_reg == VOUT_COMMAND_1801V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_UV_WARN_LIMIT, VOUT_UV_WARN_LIMIT_1702V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_UV_WARN_LIMIT)\n",
    "print(\"Set VOUT_UV_WARN_LIMIT 1.702V %s.\" %(\"successfully\"if temp_reg == VOUT_UV_WARN_LIMIT_1702V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_UV_FAULT_LIMIT, VOUT_UV_FAULT_LIMIT_1651V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_UV_FAULT_LIMIT)\n",
    "print(\"Set VOUT_UV_FAULT_LIMIT 1.651V %s.\" %(\"successfully\"if temp_reg == VOUT_UV_FAULT_LIMIT_1651V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, VOUT_MARGIN_LOW, VOUT_MARGIN_LOW_1753V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, VOUT_MARGIN_LOW)\n",
    "print(\"Set VOUT_MARGIN_LOW 1.753V %s.\" %(\"successfully\"if temp_reg == VOUT_MARGIN_LOW_1753V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, POWER_GOOD_OFF, POWER_GOOD_OFF_1603V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, POWER_GOOD_OFF)\n",
    "print(\"Set POWER_GOOD_OFF 1.603V %s.\" %(\"successfully\"if temp_reg == POWER_GOOD_OFF_1603V else \"faulty\") )\n",
    "\n",
    "i2c_6.write_word_data(PMIC_ADDR, POWER_GOOD_ON, POWER_GOOD_ON_1702V)\n",
    "temp_reg = i2c_6.read_word_data(PMIC_ADDR, POWER_GOOD_ON)\n",
    "print(\"Set POWER_GOOD_ON 1.702V %s.\" %(\"successfully\"if temp_reg == POWER_GOOD_ON_1702V else \"faulty\") )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
